/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.entity.FxCmsInfo;
import com.ykht.proj.coupon.entity.FxCmsTag;
import com.ykht.proj.coupon.service.FxCmsInfoService;
import com.ykht.proj.coupon.service.FxCmsTagService;
import com.ykht.proj.coupon.vo.FxCmsTagCount;
import com.ykht.proj.coupon.vo.FxCmsTagVO;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;
import java.time.LocalDateTime;
import java.util.List;


/**
 * cms资讯标签
 *
 * @author proj code generator
 * @date 2021-02-01 16:22:52
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fxcmstag")
@Api(value = "fxcmstag", tags = "cms资讯标签管理")
public class FxCmsTagController {

	private final FxCmsTagService fxCmsTagService;

	private final FxCmsInfoService fxCmsInfoService;

	/**
	 * 分页查询
	 *
	 * @param page       分页对象
	 * @param fxCmsTagVO cms资讯标签
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	// @PreAuthorize("@pms.hasPermission('coupon_fxcmstag_view')" )
	public R getFxCmsTagPage(Page page, FxCmsTagVO fxCmsTagVO) {
		IPage<FxCmsTagVO> iPage = fxCmsTagService.queryByPage(page, fxCmsTagVO);
		return R.ok(iPage);
	}


	/**
	 * 通过id查询cms资讯标签
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coupon_fxcmstag_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(fxCmsTagService.getById(id));
	}

	/**
	 * 新增cms资讯标签
	 *
	 * @param fxCmsTag cms资讯标签
	 * @return R
	 */
	@ApiOperation(value = "新增cms资讯标签", notes = "新增cms资讯标签")
	@SysLog("新增cms资讯标签")
	@PostMapping
	//@PreAuthorize("@pms.hasPermission('coupon_fxcmstag_add')" )
	public R save(@RequestBody FxCmsTag fxCmsTag) {
		fxCmsTag.setCreateUser(SecurityUtils.getUserId());
		fxCmsTag.setCreateTime(LocalDateTime.now());
		fxCmsTag.setUpdateTime(LocalDateTime.now());
		FxCmsTag one = fxCmsTagService.getOne(Wrappers.lambdaQuery(FxCmsTag.class)
				.eq(FxCmsTag::getName, fxCmsTag.getName())
				.eq(FxCmsTag::getDelFlag, 0)
		);
		if (one != null) {
			return R.failed("当前标签已存在");
		} else {
			fxCmsTagService.save(fxCmsTag);
			return R.ok();
		}
	}

	/**
	 * 修改cms资讯标签
	 *
	 * @param fxCmsTag cms资讯标签
	 * @return R
	 */
	@ApiOperation(value = "修改cms资讯标签", notes = "修改cms资讯标签")
	@SysLog("修改cms资讯标签")
	@PutMapping
	//@PreAuthorize("@pms.hasPermission('coupon_fxcmstag_edit')" )
	public R updateById(@RequestBody FxCmsTag fxCmsTag) {
		fxCmsTag.setUpdateTime(LocalDateTime.now());
		fxCmsTag.setUpdateUser(SecurityUtils.getUserId());
		return R.ok(fxCmsTagService.updateById(fxCmsTag));
	}

	/**
	 * 通过id删除cms资讯标签
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除cms资讯标签", notes = "通过id删除cms资讯标签")
	@SysLog("通过id删除cms资讯标签")
	@DeleteMapping("/{id}")
	//@PreAuthorize("@pms.hasPermission('coupon_fxcmstag_del')" )
	public R removeById(@PathVariable Long id) {
		List<FxCmsInfo> list = fxCmsInfoService.list(Wrappers.lambdaQuery(FxCmsInfo.class)
				.eq(FxCmsInfo::getDelFlag, 0)
				.like(FxCmsInfo::getTags, id)
		);
		if (list.size() > 0) {
			return R.failed("当前标签已关联文章，无法删除！");
		} else {
			return R.ok(fxCmsTagService.removeById(id));
		}
	}

	/**
	 * 计数查询
	 *
	 * @return
	 */
	@ApiOperation(value = "计数查询", notes = "计数查询")
	@GetMapping("/count")
	//@PreAuthorize("@pms.hasPermission('coupon_fxcmssource_view')" )
	public R getFxCmsTagCount(FxCmsTagVO fxCmsTagVO) {
		FxCmsTagCount count = fxCmsTagService.queryCount(fxCmsTagVO);
		return R.ok(count);
	}

}
